<template>
  <transition :name="transitionName">
    <div v-show="visible" :style="customStyle" class="back-to-ceiling" @click="backToTop">
      <i class="el-icon-caret-top"></i>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'BackToTop',
  props: {
    visibilityHeight: {
      type: Number,
      default: 400
    },
    backPosition: {
      type: Number,
      default: 0
    },
    customStyle: {
      type: Object,
      // default: function() {
      //   return {
      //     right: '50px',
      //     bottom: '50px',
      //     width: '40px',
      //     height: '40px',
      //     'border-radius': '4px',
      //     'line-height': '45px',
      //     background: '#e7eaf1'
      //   }
      // }
    },
    transitionName: {
      type: String,
      default: 'fade'
    },
    documentId:{
      type: String
    }
  },
  data() {
    return {
      visible: false,
      interval: null,
      isMoving: false,
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll,true)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
    if (this.interval) {
      clearInterval(this.interval)
    }
  },
  methods: {
    handleScroll() {
      const elementById = document.getElementById(this.documentId);
      if (elementById) {
        const scrollTop = elementById.scrollTop;
        this.visible = scrollTop > this.visibilityHeight
      }
    },
    backToTop() {
      if (this.isMoving) return
      if (this.documentId === 'blog') {
        this.$emit('stopTagScroll','false')
      }
      const start = document.getElementById(this.documentId).scrollTop;
      let i = 0
      this.isMoving = true
      this.interval = setInterval(() => {
        const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500))
        if (next <= this.backPosition) {
          document.getElementById(this.documentId).scrollTo(0, this.backPosition)
          clearInterval(this.interval)
          this.isMoving = false
          if (this.documentId === 'blog' && !this.isMoving) {
            console.log('as');
            this.$emit('stopTagScroll','true')
          }
        } else {
          document.getElementById(this.documentId).scrollTo(0, next)
        }
        i++
      }, 16.7)
    },
    easeInOutQuad(t, b, c, d) {
      if ((t /= d / 2) < 1) return c / 2 * t * t + b
      return -c / 2 * (--t * (t - 2) - 1) + b
    }
  }
}
</script>

<style scoped>
.back-to-ceiling {
  position: fixed;
  background-color: #fff;
  right: 50px;
  bottom: 30px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: #409eff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 0 6px rgba(0,0,0,.2);
  cursor: pointer;
  z-index: 5;
}

.back-to-ceiling:hover {
  background-color: #f2f6fc;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity .5s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0
}

.back-to-ceiling .Icon {
  fill: #9aaabf;
  background: none;
}
</style>
